<?php
App::uses('AppController', 'Controller');
/**
 * Questionnaires Controller
 *
 * @property Questionnaire $Questionnaire
 */
class QuestionnairesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
            
		$this->Questionnaire->recursive = 0;
                $this->paginate = array ('group' => array('Questionnaire.q_id'));
		$this->set('questionnaires', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Questionnaire->exists($id)) {
			throw new NotFoundException('Invalid questionnaire','default',array('class'=>'alert alert-danger'));
		}
		$options = array('conditions' => array('Questionnaire.' . $this->Questionnaire->primaryKey => $id));
		$this->set('questionnaire', $this->Questionnaire->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Questionnaire->create();
			if ($this->Questionnaire->save($this->request->data)) {
				$this->Session->setFlash('The new questionnaire has been saved','default',array('class'=>'alert alert-info'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('The new questionnaire could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Questionnaire->exists($id)) {
			throw new NotFoundException('Invalid questionnaire','default',array('class'=>'alert alert-danger'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Questionnaire->save($this->request->data)) {
				$this->Session->setFlash('The questionnaire has been successfully edited','default',array('class'=>'alert alert-info'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('The questionnaire could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
			}
		} else {
			$options = array('conditions' => array('Questionnaire.' . $this->Questionnaire->primaryKey => $id));
			$this->request->data = $this->Questionnaire->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Questionnaire->id = $id;
		if (!$this->Questionnaire->exists()) {
			throw new NotFoundException('Invalid questionnaire','default',array('class'=>'alert alert-danger'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Questionnaire->delete()) {
			$this->Session->setFlash('Questionnaire deleted','default',array('class'=>'alert alert-info'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash('Questionnaire was not deleted','default',array('class'=>'alert alert-danger'));
		$this->redirect(array('action' => 'index'));
	}
        
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function form($id = null) {
		if (!$this->Questionnaire->exists($id)) {
			throw new NotFoundException('Invalid questionnaire','default',array('class'=>'alert alert-danger'));
		}
		$options = array('conditions' => array('Questionnaire.' . $this->Questionnaire->primaryKey => $id));
		$this->set('questionnaire', $this->Questionnaire->find('first', $options));
	}
        
        public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('form');
        }
}
